#pragma once

#include "Audio/Engine.Audio.h"
#include "Base/Tests/Test.Application.h"

#include "Utils/Converter/Converter.h"

DECL_RESOURCE_CONVERTER( "_bin/Engine.Utils.Converter.dll" );

using namespace Engine;
using namespace Audio;

#undef  APP_NAME
#define APP_NAME	AudioTestApp
#undef  APP_BASE
#define APP_BASE	TestApp


class APP_NAME : public APP_BASE
{
	typedef SHARED_POINTER_TYPE( APP_NAME )		AudioTestAppPtr;

private:
	AudioEngine		audioEngine;
	ResourceManager	resMngr;


public:
	APP_NAME (IPlatform *platform, EDevice::type deviceType) :
		APP_BASE( platform, deviceType ),
		audioEngine(ESS()),
		resMngr(ESS())
	{
		ESS()->GetEventSystem()->AttachEventListener(
			DelegateBuilder::Create( AudioTestAppPtr(this), &APP_NAME::_OnAudio ),		ESystemEvent::AUDIO );
		
		DESCTOP_ONLY(
			CHECK_ERR( ResConv::Run( "Engine", "_data/log.html", "_res/res_converter.ocf" ), );

			ESS()->GetFileSystem()->FindAndSetCurrentDir( "Engine/_data", 6 );
		)
	}

	~APP_NAME ()
	{
		GetPlatform()->EnableAudio( false );
		
		ESS()->GetEventSystem()->DetachEventListener(
			DelegateBuilder::Create( AudioTestAppPtr(this), &APP_NAME::_OnAudio ),		ESystemEvent::AUDIO );
	}
	
	virtual void Update (float dt) override
	{
		if ( ESS()->GetInput()->IsKeyClicked( EKey::BACK ) )
		{
			GetPlatform()->Terminate();
		}
	}

	virtual void OnEnter () override
	{
		audioEngine.Init();
		audioEngine.InitResources();

		GetPlatform()->EnableAudio( true );
	}
	
	virtual void OnExit () override
	{
		ESS()->GetResourceManager()->Clear();
	}

	void _OnAudio (const SysEvent &ev)
	{
		switch ( ev.audio.type )
		{
			case SysEvent::Audio::KILL_FOCUS :
				LOG( "Audio KILL_FOCUS", ELog::DEBUG );
				break;

			case SysEvent::Audio::SET_FOCUS :
				LOG( "Audio SET_FOCUS", ELog::DEBUG );
				break;

			case SysEvent::Audio::PAUSE :
				LOG( "Audio PAUSE", ELog::DEBUG );
				break;
		}
	}
};